// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#include "mk_header.h"
#include "mk_profiler.h"
#include "mk_engine.h"
#include "mk_error.h"
#include "mk_resource_manager.h"
#include "mk_timer.h"

namespace MK3DE
{
	extern CTexture2D* g_blendTexture;

	CProfiler::CProfiler() :
	_fps(0),
		_entityDraw(0),
		_primitiveDraw(0),
		_drawCall(0),
		_stop(FALSE),
		_lastSecondTime(0),
		_fpsCounter(0),
		_font(NULL)
	{
	}

	CProfiler::~CProfiler()
	{
		SAFE_RELEASE(_font);
	}

	BOOL CProfiler::Init()
	{
		BOOL ret = FALSE;

		char path[MAX_PATH];
		GetWindowsDirectory(path, MAX_PATH);
		sprintf(path, "%s\\fonts\\simsun.ttc", path);
		ret = GetEngine()->GetResourceManager()->GetFTFont(path, FALSE, 12, _font);
		MK_PROCESS_ERROR(ret);

		_font->SetBorderParam(FBT_BORDER, 0xff000000, 1);

		ret = TRUE;

Exit0:
		return ret;
	}

	void CProfiler::BeginFrame()
	{
		_fpsCounter++;
		DWORD now = GetEngine()->GetTimer()->GetMilliseconds();
		if (now - _lastSecondTime > 1000)
		{
			_lastSecondTime = now;
			_fps = _fpsCounter;
			_fpsCounter = 0;
		}
	}

	void CProfiler::EndFrame()
	{
		_entityDraw = 0;
		_primitiveDraw = 0;
		_drawCall = 0;
		_stop = FALSE;
	}

	void CProfiler::AddEntityDrawCount(DWORD count)
	{
		if (!_stop)
			_entityDraw += count;
	}

	void CProfiler::AddPrimitiveDrawCount(DWORD count)
	{
		if (!_stop)
			_primitiveDraw += count;
	}

	void CProfiler::AddDrawCallCount(DWORD count)
	{
		if (!_stop)
			_drawCall += count;
	}

	void CProfiler::ShowStatistics()
	{
		_stop = TRUE;

		wchar_t txt[1024];
		wsprintfW(txt, L"fps: %d", _fps);
		_font->DrawText(10, 10, 0xff80ff00, txt, 0);

		wsprintfW(txt, L"draw entity count: %d", _entityDraw);
		_font->DrawText(10, 25, 0xff80ff00, txt, 0);

		wsprintfW(txt, L"draw primitive count: %d", _primitiveDraw);
		_font->DrawText(10, 40, 0xff80ff00, txt, 0);

		wsprintfW(txt, L"draw call count: %d", _drawCall);
		_font->DrawText(10, 55, 0xff80ff00, txt, 0);

	}
}